import { IImageConstructor } from "../interfaces/IImageConstructor";
import { Bullet } from "./Bullet";

export class Enemy extends Phaser.Physics.Arcade.Sprite {
    
    private health: number;
    private speed: number;
    private lastShoot: number;


    private barrel: Phaser.GameObjects.Image;
    private lifeBar: Phaser.GameObjects.Graphics;


    private bullets: Phaser.GameObjects.Group;
    

    public getBarrel(): Phaser.GameObjects.Image {
        return this.barrel;
    }
    
    public getBullets(): Phaser.GameObjects.Group {
        return this.bullets;
    }
    

    constructor(param: IImageConstructor) {
        super(param.scene, param.x, param.y, param.texture);

        this.initContainer();
        this.scene.add.existing(this);
        this.scene.physics.world.enable(this);
    }


    private initContainer(): void {
        // variables
        this.health = 1;
        this.speed = 100;
        this.lastShoot = 0;


        // barrel
        this.barrel = this.scene.add.image(0, 0, 'barrelRed').setOrigin(0.5, 1)
            .setDepth(1)
        ;

        
        this.lifeBar = this.scene.add.graphics();
        this.redrawLifeBar();

        this.bullets = this.scene.add.group({
            active: true,
            maxSize: 10,
            runChildUpdate: true
        });


        // tweens
        this.scene.tweens.add({
            targets: this,
            props: {
                y: this.y - 200
            },

            delay: 0,
            duration: 2000,
            ease: 'Linear',
            easeParams: null,
            hold: 0,
            repeat: -1,
            repeatDelay: 0,
            yoyo: true
        });
    }
    

    update(...args: any[]): void {
        if(this.active){
            this.barrel.x = this.x;
            this.barrel.y = this.y;
            this.lifeBar.x = this.x;
            this.lifeBar.y = this.y;
            this.handleShooting();
        } else {
            this.destroy();
            this.barrel.destroy();
            this.lifeBar.destroy();
        }
    }


    private handleShooting(): void {
        // shoot every 1.5 seconds
        if(this.scene.time.now > this.lastShoot){
            if(this.bullets.getLength() < 10){
                this.bullets.add(
                    new Bullet({
                        scene: this.scene,
                        rotation: this.barrel.rotation,
                        x: this.barrel.x,
                        y: this.barrel.y,
                        texture: 'bulletRed'
                    })
                );

                this.lastShoot = this.scene.time.now + 400;
            }
        }
    }


    private redrawLifeBar(): void {
        this.lifeBar.clear();

        this.lifeBar.fillStyle(0xe66a28, 1)
            .fillRect(
                // -this.width/2 * common_scale, 
                -this.width/2,
                // this.height/2 * common_scale, 
                this.height/2,
                // this.width * this.health * common_scale, 
                this.width * this.health, 
                // 15 * common_scale
                15
            )
            .lineStyle(2, 0xffffff)
            .strokeRect(
                // -this.width/2 * common_scale, 
                -this.width/2,
                // this.height/2 * common_scale, 
                this.height/2,
                // this.width * common_scale,
                this.width,
                // 15*common_scale
                15
            )
            .setDepth(1)
        ;
    }
    
    
    public updateHealth(): void {
        if(this.health > 0){
            this.health -= 0.05;
            this.redrawLifeBar();
        } else {
            this.health = 0;
            this.active = false;
        }
    }
}